'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 士兵子项
 * @author sundbcn@gmail.com (sun debing)
 */
var _ = require('lodash');
var mongoose = require('mongoose');
var dataApi = require('../../libs/dataApi');
var soldierConf = require('../../config/soldierConf');

///////////////////////////////////////////////////////////
/**
 * 士兵子项
 */
var soldierItemSchema = new mongoose.Schema({
    // 士兵类型
    soldier_type: {
        type: Number,
        required: true
    },

    // 等级
    lv: {
        type: Number,
        default: 1
    },

    // 星级
    star: {
        type: Number,
        default: 1
    },

    // 士兵天赋
    talent: {
        type: [Number]
    },

    // 星级达到走马灯条件，发公告（是否发过公告记录）
    is_star_msg: {
        type: Array
    }
});

/**
 * 获取马灯信息是否发送状态
 * @return {[type]} [description]
 */
soldierItemSchema.methods.getIsStarMsg = function (value) {
    return _.indexOf(this.is_star_msg, value) == -1;
};

/**
 * 设置马灯条件发送状态
 * @return {[type]} [description]
 */
soldierItemSchema.methods.setIsStarMsg = function (value) {
    if (!!this.getIsStarMsg(value)) {
        this.is_star_msg.push(value);
    }
};

/**
 * 士兵类型
 * @return {[type]} [description]
 */
soldierItemSchema.methods.getType = function () {
    return this.soldier_type;
};

/**
 * 士兵等级
 * @return {[type]} [description]
 */
soldierItemSchema.methods.getLv = function () {
    return this.lv;
};

/**
 * 士兵精锐等级
 * @return {[type]} [description]
 */
soldierItemSchema.methods.getStar = function () {
    return this.star;
};

/**
 * 升级士兵精锐等级
 * @return {[type]} [description]
 */
soldierItemSchema.methods.upgradeStar = function () {
    this.star++;
    this.talent[this.star - 1]++;
    this.markModified('talent');
};

/**
 * 士兵升级
 * @return {[type]} [description]
 */
soldierItemSchema.methods.upgrade = function () {
    this.lv++;
    return true;
};

/**
 * 升级天赋
 * @return {[type]} [description]
 */
soldierItemSchema.methods.upgradeTalent = function (star_lv) {
    this.talent[star_lv]++;
    this.markModified('talent');
    return true;
};

/**
 * 初始化天赋
 * @return {[type]} [description]
 */
soldierItemSchema.methods.initTalent = function (star_lv) {
    var self = this;
    var talent_type_lv_list = soldierConf.getTalentTypeLvList(self.soldier_type);
    self.talent = [];
    var need_star_lv = 0;
    _.forEach(talent_type_lv_list, function (talent_type_lv) {
        need_star_lv++;
        if (self.star >= need_star_lv) {
            // 开启对应开赋
            talent_type_lv++;
        }
        self.talent.push(talent_type_lv);
    });

    return true;
};

module.exports = soldierItemSchema;
